package com.alm.handle.service.impl;

import com.alm.common.core.redis.RedisCache;
import com.alm.common.utils.DateUtils;
import com.alm.handle.core.domain.entity.SysDevCurrentData;
import com.alm.handle.core.domain.entity.SysDevHistoryInfo;
import com.alm.handle.core.domain.entity.vo.CurrentVo;
import com.alm.handle.mapper.SysDevCurrentDataMapper;
import com.alm.handle.mapper.SysDevHistoryInfoMapper;
import com.alm.handle.service.ISysDevHistoryInfoService;
import com.alm.handle.util.SoketEleKey;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysDevHistoryInfoServiceImpl implements ISysDevHistoryInfoService {
    @Autowired
    RedisCache redisCache;
    @Autowired
    SysDevHistoryInfoMapper historyInfoMapper;
    @Autowired
    SysDevCurrentDataMapper currentDataMapper;
    @Override
    public void insertHistory(CurrentVo currentVo) {
        SysDevHistoryInfo historyInfo=new SysDevHistoryInfo();
        historyInfo.setDeviceId(currentVo.getDeviceMac());
        historyInfo.setVoltage(new BigDecimal(currentVo.getVoltage()));
        if(currentVo.getCurrent()!=null)
        historyInfo.setElectricCurrent(new BigDecimal(currentVo.getCurrent()));
        if(currentVo.getActivePower()!=null)
        historyInfo.setActivePower(new BigDecimal(currentVo.getActivePower()));
        if(currentVo.getFrequency()!=null)
        historyInfo.setFrequency(new BigDecimal(currentVo.getFrequency()));
        if(currentVo.getPowerFactor()!=null)
        historyInfo.setPowerFactor(new BigDecimal(currentVo.getPowerFactor()));
        if(currentVo.getEnergy()!=null)
        historyInfo.setEnergy(new BigDecimal(currentVo.getEnergy()));
        historyInfo.setWorkStatus(currentVo.getWorkStatus());
        historyInfo.setYear(currentVo.getYear());
        historyInfo.setMonth(currentVo.getMonth());
        historyInfo.setDay(currentVo.getDay());
        historyInfo.setCreatTime(System.currentTimeMillis());
        historyInfoMapper.insert(historyInfo);
    }
    @PostConstruct
    private void  init(){
        initSaveHistoryDev();
    }
    @Override
    public void initSaveHistoryDev() {
        redisCache.deleteObject(SoketEleKey.SAVE_HIS);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("save_his",1);
        queryWrapper.select("device_id");
        Set<String> set=new HashSet<>();
        List<SysDevCurrentData> list=currentDataMapper.selectList(queryWrapper);
        if(list!=null&&list.size()>0){
            for(SysDevCurrentData data : list){
                set.add(data.getDeviceId().toUpperCase());
            }
        }
        redisCache.setCacheSet(SoketEleKey.SAVE_HIS,set);
    }

    @Override
    public List<SysDevHistoryInfo> selectDevHisList(SysDevHistoryInfo sysDevHistoryInfo) {
//        List<Date> searchTimeSlot = sysDevHistoryInfo.getSearchTimeSlot();
//        searchTimeSlot.forEach(time -> {
//
//        });
        List<SysDevHistoryInfo> sysDevHistoryInfoList = historyInfoMapper.selectDevHisList(sysDevHistoryInfo);
        return sysDevHistoryInfoList;
    }

    @Override
    public List<SysDevHistoryInfo> selectDevHisList() {
        return historyInfoMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<SysDevHistoryInfo> getHistory(Long startTIme, Long endTime, String devmc) {
        return null;
    }
}
